package app.sceledroid.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import app.sceledroid.R;
import app.sceledroid.entity.Profile;
import app.sceledroid.network.SearchHandler;

public class SearchActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		Button searchButton = (Button) findViewById(R.id.button1);
		final EditText searchText = (EditText) findViewById(R.id.editText1);
		searchText.setText("ad");
		
		searchButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//ArrayList<Profile> result = search.searchProfile(searchText.getText().toString());
				Bundle bundle = new Bundle();
				//bundle.putStringArrayList("param", result);
				bundle.putString("key", searchText.getText().toString());
				Intent intent = new Intent(SearchActivity.this, DirectEmailActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.afterlogin_menu, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//intent for settings
		Intent intent  = new Intent(SearchActivity.this, SettingsActivity.class);
		//toast for others
		Context context = getApplicationContext();
		CharSequence help = "SceleDroid";
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(context, help, duration);
		
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.help:
	       // showHelp();
	    	toast.show();
	        return true;
	    case R.id.settings:
	    	startActivity(intent);
	    	return true;
	    case R.id.credits:
	    	toast.show();
	    	//credits here;
	    	return true;
	    case R.id.logout:
	    	intent = new Intent(SearchActivity.this, LoginActivity.class);
	    	startActivity(intent);
	    	SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
			Editor e = pref.edit();//this.getPreferences(Context.MODE_PRIVATE).edit();
			e.clear();
			e.commit();
			intent = new Intent(this, LoginActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
	    	return true;
	    case R.id.search:
	    	intent = new Intent(SearchActivity.this, SearchActivity.class);
	    	startActivity(intent);
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}
